package com.kinon.gfcanteen_v1;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;

import com.kinon.gfcanteen_v1.view.fragment.BalanceFragment;
import com.kinon.gfcanteen_v1.view.fragment.OrderFragment;
import com.kinon.gfcanteen_v1.view.fragment.RechargeFragment;
import com.kinon.gfcanteen_v1.view.fragment.SettelmentFragment;
import com.kinon.gfcanteen_v1.view.fragment.TakemealFragment;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private RadioButton rb_settlement;
    private RadioButton rb_take;
    private RadioButton rb_order;
    private RadioButton rb_balance;
    private RadioButton rb_recharge;
    private FrameLayout fl_home;
    private ArrayList<Fragment> fragmentList;
    private FragmentManager fm=null;
    //fragment的下标
    private int fragmentIndex=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        rb_settlement= (RadioButton) findViewById(R.id.rb_settlement);
        rb_take= (RadioButton) findViewById(R.id.rb_take);
        rb_order= (RadioButton) findViewById(R.id.rb_order);
        rb_balance= (RadioButton) findViewById(R.id.rb_balance);
        rb_recharge= (RadioButton) findViewById(R.id.rb_recharge);
        fl_home= (FrameLayout) findViewById(R.id.fl_home);
        addFragment();
        addListener();
    }
    private void addListener(){
        rb_settlement.setOnClickListener(this);
        rb_take.setOnClickListener(this);
        rb_order.setOnClickListener(this);
        rb_balance.setOnClickListener(this);
        rb_recharge.setOnClickListener(this);
    }
    private void addFragment(){
        fragmentList=new ArrayList<>();
        fragmentList.add(new SettelmentFragment());
        fragmentList.add(new TakemealFragment());
        fragmentList.add(new OrderFragment());
        fragmentList.add(new BalanceFragment());
        fragmentList.add(new RechargeFragment());
        fristShowFragment();
    }

    private void fristShowFragment(){
        fm=getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.fl_home, fragmentList.get(0));
        fragmentTransaction.commit();
    }

    private void showFragment(int position){
        if(fragmentIndex!=position){
            FragmentTransaction transaction=fm.beginTransaction();
            transaction.hide(fragmentList.get(fragmentIndex));
            //没有加载过要显示的fragment
            if(!fragmentList.get(position).isAdded()){
                transaction.add(R.id.fl_home,fragmentList.get(position));
            }
            transaction.show(fragmentList.get(position));
            transaction.commit();
            fragmentIndex=position;
        }
    }




    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rb_settlement:
                showFragment(0);
            break;
            case R.id.rb_take:
                showFragment(1);
            break;
            case R.id.rb_order:
                showFragment(2);
            break;
            case R.id.rb_balance:
                showFragment(3);
            break;
            case R.id.rb_recharge:
                showFragment(4);
            break;
        }
    }
}
